package com.example.wx.demo.wx;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author zhangd
 */
@Slf4j
@Component
public class TokenTask {
    /**
     * 定义 AccountTokenService
     */
    private final AccountTokenService accountTokenService;

    /**
     * 构造方法
     *
     * @param accountTokenService AccountTokenService
     */
    @Autowired
    public TokenTask(AccountTokenService accountTokenService) {
        this.accountTokenService = accountTokenService;
    }

    /**
     * 设置服务Token
     */
    @Scheduled(fixedDelay = 60 * 60 * 1000)
    void setServerToken() {
        //设置access——token
        final String accessToken = accountTokenService.getWxToken();
        TokenSingleton.getTokenSingleton().setWeixinToken(accessToken);
        //设置jsapi——ticket
        TokenSingleton.getTokenSingleton().setJsApiTicket(accountTokenService.getJsApiTicket(accessToken));
        // 打印 Token 日志
        log.info("服务凭证设置完成,当前凭证:{}", TokenSingleton.getTokenSingleton());
    }
}
